package gmomo.test.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import gmomo.GMoMoConfig;
import gmomo.GMoMoConnection;
import gmomo.PacketListenerImpl;

import javax.swing.JButton;
import javax.swing.JFrame;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;

public class MainFrame extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 2336620923194614831L;
	
	private GMoMoConnection connection;
	private ContactList contactList;
	private JButton btnLogin;
	private PacketListenerImpl listener;
	
	private final static int NOT_CONNECTED_STATE = 0;
	private final static int LOGIN_STATE = 1;
	
	private int state = NOT_CONNECTED_STATE;
	
	private Map<String, MessageFrame> msgWin = new HashMap<String, MessageFrame>();

	public MainFrame() {
		super("GMoMo Test");
		setupGUI();
		setupGMoMo();
	}
	
	private void setupGMoMo() {
		try {
			connection = new GMoMoConnection( GMoMoConfig.load("ext/gmomo/config/config.properties") );
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void setupListener() {
		listener = new PacketListenerImpl();
		listener.addPresenceListener(new GUIPresenceListener(this));
		listener.addInvitationListener(new GUIInvitationListener(this));
		listener.addMessageListener(new GUIMessageListener(this));
		
		connection.addPacketListener(listener, null);
	}
	
	private void setupGUI() {
		contactList = new ContactList(this);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(contactList, BorderLayout.CENTER);
		
		btnLogin = new JButton("Connect");
		contentPane.add(btnLogin, BorderLayout.SOUTH);
		btnLogin.addActionListener(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setPreferredSize(new Dimension(300, 300));
		setVisible(true);
	}
	
	public void updateMsgWin(Contact contact) {
		MessageFrame frame = msgWin.get( contact.getXmppAddress() );
		if ( frame == null ) {
			frame = new MessageFrame(this, contact);
			msgWin.put( contact.getXmppAddress() , frame);
		}else {
//			frame.setVisible(true);
			frame.showFrame();
		}
	}
	
	public void updateMsgWin(String from, String text) {
		MessageFrame frame = msgWin.get( from );
		if ( frame == null ) {
			frame = new MessageFrame(this, new Contact(from));
			msgWin.put( from , frame);
		}else {
			frame.setVisible(true);
		}
		frame.update(text);
	}
	
	public void login(String uid, String pwd) {
		try {
			connection.connect();
			connection.login(uid, pwd);
			
			state = LOGIN_STATE;
			btnLogin.setText("Disconnect");
			
			setupListener();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void login(String uid, String pwd, String resource) {
		try {
			connection.connect();
			connection.login(uid, pwd, resource);
			
			state = LOGIN_STATE;
			btnLogin.setText("Disconnect");
			
			setupListener();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updatePresence(Presence presence) {
		contactList.updateContact(presence);
	}
	
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == btnLogin ) {
			if ( state == NOT_CONNECTED_STATE ) {
				new LazyLoginDialog(this);
			}else {
				connection.disconnect();
				btnLogin.setText("Connect");
				contactList.clear();
				state = NOT_CONNECTED_STATE;
			}
		}
	}
	
	public void send(Packet packet) {
		connection.sendPacket(packet);
	}
	
	public static void main(String args[]) {
		MainFrame app = new MainFrame();
	}
	
}//end of class MainFrame
